package com.efounder.adapter;

import android.content.Context;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.efounder.bean.Address;
import com.efounder.mobilemall.R;

import java.util.List;

/**
 * @deprecated
 * Created by will on 18-1-24.
 * 收货地址适配器
 */

public class AddressAdapter extends RecyclerView.Adapter<AddressAdapter.AddressViewHolder> {

    private List<Address> addresses;
    private Context mContext;
    private OnShowSubmitPopupListener onShowSubmitPopupListener;
    private OnShowAddAddressPopupListener onShowAddAddressPopupListener;
    private OnShowEditAddressPopupListener onShowEditPopupListener;
    private OnAddressSelectedListener onAddressSelectedListener;
    private static final int VIEW_TYPE_ADDRESS = 1;
    public static final int VIEW_TYPE_FOOTER = 2;
    private View footerView;

    public AddressAdapter(List<Address> addresses, Context mContext) {
        this.addresses = addresses;
        this.mContext = mContext;
    }

    @Override
    public AddressViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == VIEW_TYPE_FOOTER){
            footerView = LayoutInflater.from(mContext).inflate(R.layout.footer_add_address, parent, false);

            return new AddressViewHolder(footerView);
        }else {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_address_list, parent, false);

            return new AddressViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(AddressViewHolder holder, final int position) {
        if (position == addresses.size()) {
            holder.ivAddAddress.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onShowAddAddressPopupListener.onShowAddAddressPop();
                }
            });
        }else {
            holder.tvName.setText(addresses.get(position).getUserName());
            holder.tvPhone.setText(addresses.get(position).getPhone());
            holder.tvAddress.setText(addresses.get(position).getArea() + " " + addresses.get(position).getAddress());
            if (addresses.get(position).getIsSelected().equals("1")){
                holder.tvDefault.setVisibility(View.VISIBLE);
            }else {
                holder.tvDefault.setVisibility(View.GONE);
            }
            holder.ivEdit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onShowEditPopupListener.onShowEditPop(position);
                }
            });
            if (addresses.get(position).isCurrentSelected()){
                holder.itemView.setBackground(ContextCompat.getDrawable(mContext, R.color.darkgray));
                holder.ivSelected.setVisibility(View.VISIBLE);
            }else {
                holder.itemView.setBackground(ContextCompat.getDrawable(mContext, R.color.white));
                holder.ivSelected.setVisibility(View.INVISIBLE);
            }
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onAddressSelectedListener.onAddressSelected(position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return addresses.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position==addresses.size()){
            return VIEW_TYPE_FOOTER;
        }else {
            return VIEW_TYPE_ADDRESS;
        }
    }

    class AddressViewHolder extends RecyclerView.ViewHolder{

        ImageView ivEdit;
        ImageView ivSelected;
        TextView tvName;
        TextView tvPhone;
        TextView tvAddress;
        TextView tvDefault;
        ImageView ivAddAddress;

        public AddressViewHolder(View itemView) {
            super(itemView);
            if (itemView == footerView) {
                ivAddAddress = (ImageView) itemView.findViewById(R.id.iv_add_address);
            }else {
                ivEdit = (ImageView) itemView.findViewById(R.id.iv_edit_address);
                ivSelected = (ImageView) itemView.findViewById(R.id.iv_selected);
                tvName = (TextView) itemView.findViewById(R.id.tv_name);
                tvPhone = (TextView) itemView.findViewById(R.id.tv_phone);
                tvAddress = (TextView) itemView.findViewById(R.id.tv_address);
                tvDefault = (TextView) itemView.findViewById(R.id.tv_default);
            }

        }
    }

    public interface OnShowSubmitPopupListener{
        void onShowSubmitPop();
    }

    public void setOnShowSubmitPopupListener(OnShowSubmitPopupListener onShowSubmitPopupListener) {
        this.onShowSubmitPopupListener = onShowSubmitPopupListener;
    }

    public interface OnShowAddAddressPopupListener{
        void onShowAddAddressPop();
    }

    public void setOnShowAddAddressPopupListener(OnShowAddAddressPopupListener onShowAddAddressPopupListener) {
        this.onShowAddAddressPopupListener = onShowAddAddressPopupListener;
    }

    public interface OnShowEditAddressPopupListener{
        void onShowEditPop(int position);
    }

    public void setOnShowEditPopupListener(OnShowEditAddressPopupListener onShowEditPopupListener) {
        this.onShowEditPopupListener = onShowEditPopupListener;
    }

    public interface OnAddressSelectedListener{
        void onAddressSelected(int position);
    }

    public void setOnAddressSelectedListener(OnAddressSelectedListener onAddressSelectedListener) {
        this.onAddressSelectedListener = onAddressSelectedListener;
    }
}
